<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="消防员id" prop="firefighterId">
				<el-input v-model="queryParams.firefighterId" placeholder="请输入消防员id" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="消防员姓名" prop="firefighterName">
				<el-input v-model="queryParams.firefighterName" placeholder="请输入" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="消防员证件号" prop="firefighterIdCard">
				<el-input v-model="queryParams.firefighterIdCard" placeholder="请输入消防员证件号" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['drill:drill:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['drill:drill:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['drill:drill:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['drill:drill:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="drillList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="id" align="center" prop="id" />
			<el-table-column label="重大演习演练" align="center" prop="drill" />
			<el-table-column label="消防员id" align="center" prop="firefighterId" />
			<el-table-column label="消防员姓名" align="center" prop="firefighterName" />
			<el-table-column label="消防员证件号" align="center" prop="firefighterIdCard" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['drill:drill:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['drill:drill:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改奖励演练对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" @submit.native.prevent>

				<div style="width: 100%; display: flex;justify-content: flex-end;padding-bottom: 20px;"
					v-if="form.id == null">
					<el-button @click="addInput">添加重大演习输入框</el-button>
				</div>

				<el-form-item v-if="form.id == null">
					<span class="biaoti">重大演习演练</span>
					<div v-for="(item, i) in form.firefighterAwards" :key="i">
						<div style="padding-bottom: 20px;padding-top: 20px;">
							<div style="display: flex;margin-bottom:20px ;">
							<span style="width: 10%;text-align: center;line-height: 40px;">时间</span>
							<el-date-picker @change="addsMonth(i, $event)" v-model="item.time" type="month"
								placeholder="选择月">
							</el-date-picker>
							</div>

							<div style="display: flex;margin-bottom:20px ;">
							<span style="width: 10%;text-align: center;line-height: 40px;">级别</span>
							<el-select v-model="item.levles" placeholder="请选择" @change="addsLevel(i,$event)">
								<el-option v-for="item in levelList" :key="item.rewardId" :label="item.rewardName"
									:value="item.rewardId">
								</el-option>
							</el-select>
							</div>

							<div style="display: flex;margin-bottom:20px ;">
							<span style="width: 10%;text-align: center;line-height: 40px;">演习演练</span>
							<el-input :value="item.name" placeholder="请输入" @input="addsValue(i, $event)" />
							</div>
							</div>
					</div>
				</el-form-item>

				<el-form-item label="" prop="name" v-if="form.id != null">
					<span class="biaoti">重大演习演练</span>
					<el-input v-model="form.firefighterAwards[0].name" placeholder="请输入" />
				</el-form-item>

				<el-form-item label="" prop="time" v-if="form.id != null">
					<span class="biaoti">时间</span>
					<el-input v-model="form.firefighterAwards[0].time" placeholder="请输入" />
				</el-form-item>

				<el-form-item label="" prop="levles" v-if="form.id != null">
					<span class="biaoti">等级</span>
					<div>
						<el-select v-model="form.firefighterAwards[0].levles" placeholder="请选择">
							<el-option v-for="item in levelList" :key="item.rewardId" :label="item.rewardName"
								:value="item.rewardId">
							</el-option>
						</el-select>
					</div>

					<!-- <el-input v-model="" placeholder="请输入" /> -->
				</el-form-item>

				<el-form-item label="" prop="firefighterName">
					<span class="biaoti">消防员姓名</span>
					<el-input v-model="form.firefighterName" placeholder="请输入" />
				</el-form-item>

				<el-form-item label="" prop="firefighterIdCard">
					<span class="biaoti">消防员证件号</span>
					<el-input v-model="form.firefighterIdCard" placeholder="请输入消防员证件号" @change="getFirefighterIdCard" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listDrill,
		getDrill,
		delDrill,
		addDrill,
		updateDrill,
		getFirefighterIdCard,
		addDrillList
	} from "@/api/drill/drill";

	export default {
		name: "Drill",
		data() {
			return {
				levelList: [{
						rewardId: "1",
						rewardName: "优秀"
					},
					{
						rewardId: "2",
						rewardName: "良好"
					},
					{
						rewardId: "3",
						rewardName: "合格"
					}
				],
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 重大演习演练表格数据
				drillList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					drill: null,
					firefighterId: null,
					firefighterName: null,
					firefighterIdCard: null
				},
				// 表单参数
				form: {
					firefighterName: "",
					firefighterIdCard: "",
					firefighterAwards: [{
						name: "",
						levles: "",
						time: ""
					}, ],
				},
				radio: 1,
				// 表单校验
				rules: {

				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			getFirefighterIdCard(nameId) {
				console.log("获取证件号：", nameId)
				getFirefighterIdCard(nameId).then(res => {
					console.log("证件号：", res)
					this.form.firefighterName = res.data.name
				})
			},
			// 批量添加
			addsUpdate() {
				console.log("表单数据：", this.form)
				addAwardList(this.form).then(res => {
					console.log("返回结果：", res)
				})
				this.getList();
			},
			addInput() {
				// 向inputs数组中添加一个新的输入框对象
				this.form.firefighterAwards.push({
					time: "",
					levles: "",
					name: "",
				});
			},
			// 更新特定输入框的值
			addsValue(i, value) {
				console.log(value)
				this.form.firefighterAwards[i].name = value;
			},
			addsLevel(i, level) {
				this.form.firefighterAwards[i].levles = level;
			},

			addsMonth(i, e) {
				this.form.firefighterAwards[i].time = this.MonthUpdate(e)
			},
			MonthUpdate(e) {
				var formattedDate = e;
				// 解析日期时间字符串为Date对象
				const date = new Date(formattedDate);
				// 获取年份
				const year = date.getFullYear();
				// 获取月份（0-11，所以要加1）
				const month = date.getMonth() + 1;
				// 格式化月份为两位数字
				const formattedMonth = month < 10 ? '0' + month : month;
				// 构建最终的日期字符串
				formattedDate = `${year}-${formattedMonth}`;
				console.log(formattedDate); // 输出: 2024-10
				return formattedDate
			},
			/** 查询重大演习演练列表 */
			getList() {
				this.loading = true;
				listDrill(this.queryParams).then(response => {
					this.drillList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					firefighterName: "",
					firefighterIdCard: "",
					firefighterAwards: [{
						name: "",
						levles: "",
						time: ""
					}, ],
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加重大演习演练";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getDrill(id).then(response => {
					this.form.id = response.data.id;
					this.form.firefighterName = response.data.firefighterName;
					this.form.firefighterIdCard = response.data.firefighterIdCard;
					this.form.firefighterAwards[0].name = response.data.drill;
					this.form.firefighterAwards[0].levles = response.data.levles;
					this.form.firefighterAwards[0].time = response.data.time;
					this.open = true;
					this.title = "修改重大演习演练";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateDrill(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							console.log("新增表单数据：", this.form)
							addDrillList(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除重大演习演练编号为"' + ids + '"的数据项？').then(function() {
					return delDrill(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('drill/drill/export', {
					...this.queryParams
				}, `drill_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>

<style>
.biaoti {
		font-weight: bold;
		font-size: 20px;
	}
</style>